// var crePopenObj, ajax_headers = sessionStorage.getItem('token') == null ? null : sessionStorage.getItem('token') //创建弹框对象
var crePopenObj, ajax_headers = localStorage.getItem('token') == null ? null : localStorage.getItem('token') //创建弹框对象
resUrl = {
        layuiTabCols: "/layuiTabCols/seleTab",
        mmfCRUD: "name",
        erWpCURD: "name",
        colorCURD: "colorName",
        sizeCURD: "name",
        depCRUD: "deptName",
        erMpsCRUD: "mpsid",
        materialCRUD: "name"
    }
    // ajax_headers = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MTUzMzg0MDMsInVzZXJuYW1lIjoiYWRtaW4ifQ.gQt5QjluCtzIZS6xauGiwxaseFFRQBaNL3HlCk8zYEw"

/**
 * 详情 https://www.layui.com/doc/modules/layer.html#type
 * @param {*标题} title 
 * @param {*基本层类型 可传入的值有：0（信息框）1（页面层，默认）2（iframe层）3（加载层）4（tips层）} type 
 * @param {*坐标 类型：String/Array，默认：垂直水平居中 offset: 'auto' offset: ['100px', '50px']} offset 
 * @param {*内容} content 
 * @param {*按钮} btn 
 * @param {*按钮排列 类型：String，默认：c} btnAlign 
 * @param {*是否点击遮罩关闭 类型：Boolean，默认：false} shadeClose 
 * @param {*遮罩} shade 
 * @param {*宽高 类型：String/Array，默认：['50%', '50%']} area 
 * @param {* 弹出动画 类型：Number，默认：0} anim 
 * @param {* 是否允许多个tips 类型：Boolean，默认：false} tipsMore 
 * @param {*默认不显示最大小化按钮 类型：Boolean，默认：false} maxmin 
 * @param {*层弹出后的成功回调方法} successFun 
 * @param {*弹框销毁方法} createPopenFunEnd 
 */
function createPopenFun(
    title, type, offset, content, btn,
    btn_yes, btn_yes2, btn_yes3, btnAlign,
    shadeClose, shade, area, anim, tipsMore, maxmin,
    resizingFum, successFun, createPopenFunEnd) {

    title = title == null ? "创建" : title;
    type = type == null ? 1 : type;
    offset = offset == null ? "auto" : offset;
    btnAlign = btnAlign == null ? "c" : btnAlign;
    shade = shade == null ? 0.5 : shade;
    area = area == null ? ['50%', '50%'] : area;
    anim = anim == null ? 0 : anim;
    maxmin = maxmin == null ? false : maxmin;
    shadeClose = shadeClose == null ? true : shadeClose;
    tipsMore = tipsMore == null ? false : tipsMore;
    let end = createPopenFunEnd == null ? function() {} : createPopenFunEnd;
    let success = successFun;
    let resizing = resizingFum;
    let yes = btn_yes;
    let btn2 = btn_yes2;
    let btn3 = btn_yes3;

    return layer.open({
        title: title,
        type: type,
        offset: offset,
        content: content,
        anim: anim,
        btn: btn,
        yes: function(index, layero) {
            //按钮【按钮一】的回调
            yes(index, layer);
        },
        btn2: function(index, layero) {
            //按钮【按钮二】的回调
            btn2(index, layer);

            return false //开启该代码可禁止点击该按钮关闭
        },
        btn3: function(index, layero) {
            //按钮【按钮三】的回调
            btn3(index, layer);
            return false //开启该代码可禁止点击该按钮关闭
        },
        shadeClose: shadeClose,
        resizing: function(layero) {
            resizing(layero)
        },
        btnAlign: btnAlign, //按钮居中,
        shade: shade, //不显示遮罩,
        // tipsMore: true,
        area: area,
        tipsMore: tipsMore,
        maxmin: maxmin,
        success: function(layero, index) {
            success(layero, index);
        },
        end: function() { //弹框销毁时执行
            end();
        }
    });
}


/**
 * @param {*请求地址} url
 * @param {*请求参数} data
 * @param {*请求类型} type
 * @param {*请求是否同步 默认值: true。默认设置下，所有请求均为异步请求。如果需要发送同步请求，请将此选项设置为 false} async
 * @param {*请求是否缓存 设置为 false 将不缓存此页面} cache
 * @param {*请求成功后的回调} successFun
 * @param {*请求失败后的回调} errorFun
 */
function AJAX(url, data, type, async, cache, successFun, errorFun) {
    let error = errorFun == null ? function() {} : errorFun,
        success = successFun == null ? function() {} : successFun;
    async = async == null ? true : async;
    cache = cache == null ? true : cache;
    let contentType = 'application/json;charset=utf8';
    // console.log("令牌", ajax_headers);
    ajax_headers = localStorage.getItem('token') == null ? null : localStorage.getItem('token')
    switch (type) {
        case "GET":
            contentType = 'application/x-www-form-urlencoded';
            break;
        default:
            break;
    }

    return $_$.ajax({
        url,
        data,
        type,
        async, //默认值: true。默认设置下，所有请求均为异步请求。如果需要发送同步请求，请将此选项设置为 false。
        cache, //设置为 false 将不缓存此页面
        dataType: "json",
        contentType,
        headers: {
            "jwt": ajax_headers
        },
        crossDomain: true == !(document.all),
        success: function(res, status, xhr) {
            //console.log('head: ', xhr.getAllResponseHeaders())
            //console.log('res', res)
            success(res);
            switch (res.code) {
                case 4400:
                case 4002:
                    skipLogin();
                    break;
                default:
                    break;
            }
            //关闭父类加载弹框
            //globalParentLoadingFunClose();
            // layer.closeAll();
            if (res.code != 200) {
                layer.msg(res.msg, {
                    icon: 5,
                    time: 2000
                });
            }
        },
        error: function(res) {
            switch (res.code) {
                case 4400:
                case 4002:
                    skipLogin();
                    break;
                default:
                    break;
            }
            layer.closeAll();
            layer.msg("操作失败，请稍后再操作", {
                icon: 5,
                time: 2000
            });
            error();
        }
    }).fail(function(res, textStatus, errorThrown) {
        //net::ERR_CONNECTION_REFUSED 发生时，也能进入
        //关闭父类加载弹框
        globalParentLoadingFunClose();
        layer.closeAll();
        layer.msg("操作失败，请稍后再操作", {
            icon: 5,
            time: 2000
        });

        window.parent.parent.setProgressFun(null, 100)
    });



}

//关闭加载弹框方法
function globalParentLoadingFunClose(index) {
    try {
        //关闭加载弹框
        if (index == null) {
            window.parent.globalLoadingFunClose();
        } else {
            layer.close(index);
        }
    } catch (error) {
        console.log("加载弹框出错");
    }
}

//加载弹框
function globalParentLoadingFun() {
    try {
        //加载弹框
        window.parent.globalLoadingFun();
    } catch (error) {
        console.log("加载弹框出错");
    }
}

//设置头部token
function setHeadersToken(token) {
    ajax_headers = token == undefined ? sessionStorage.getItem('token') : token;
}

//跳转登录页
function skipLogin() {
    window.parent.parent.setProgressFun(null, 100)
    layer.closeAll();
    console.log('方法执行')
    window.parent.location.href = '/login.html';
}